{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\westone\\AppData\\Local\\Temp\\ipykernel_24624\\3711731740.py:6: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  name=k[1][1]\n",
      "C:\\Users\\westone\\AppData\\Local\\Temp\\ipykernel_24624\\3711731740.py:7: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  str=k[1][2]\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd;\n",
    "\n",
    "color_table = dict()\n",
    "color_data = pd.read_csv('css-color-name.csv')\n",
    "for k in color_data.iterrows():\n",
    "    name=k[1][1]\n",
    "    str=k[1][2]\n",
    "    r = int(str[1:3], 16)\n",
    "    g = int(str[3:5], 16)\n",
    "    b = int(str[5:7], 16)\n",
    "    color_table[name] = (r, g, b)\n",
    "\n",
    "decls = []\n",
    "defines = []\n",
    "elems = []\n",
    "for k, v in color_table.items():\n",
    "    decls.append('extern const Color {};\\n'.format(k))\n",
    "    defines.append('const Color {}({}, {}, {});\\n'.format(k, v[0], v[1], v[2]))\n",
    "    elems.append('{{\"{}\", &{}}},\\n'.format(k, k))\n",
    "\n",
    "with open('csv_names.cpp','w',encoding = 'utf-8') as f:\n",
    "    f.write('// csv_decls\\n')\n",
    "    f.writelines(decls)\n",
    "    f.write('\\n')\n",
    "    f.write('// csv_defines\\n')\n",
    "    f.writelines(defines)\n",
    "    f.write('\\n')\n",
    "    f.write('// csv_elems\\n')\n",
    "    f.writelines(elems)\n",
    "    f.write('\\n')\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
